package orchestration.android.parcelable;

import orchestration.businessprocess.execution.Property;
import orchestration.businessprocess.model.BPArtifact;
import orchestration.businessprocess.model.BPArtifactId;
import android.os.Parcel;
import android.os.Parcelable;

public class ParcelableArtifact extends BPArtifact implements Parcelable {

	public ParcelableArtifact(BPArtifactId id, String name, Class<?> type) {
		super(id, name, type);
	}
	public ParcelableArtifact(BPArtifactId id, String name, Class<?> type,Class<?> componentType) {
		super(id, name, type,componentType);
	}
	public ParcelableArtifact(BPArtifactId id) {
		super(id);
	}
	@Override
	public Property createProperty() {
		return new ParcelableProperty(this);
	}
	
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(getId().getValue());
		dest.writeString(getName());
		dest.writeString(getType().getSimpleName());
		if(getComponentType()!=null)
			dest.writeString(getComponentType().getSimpleName());
	}
	
	public static final Parcelable.Creator<ParcelableArtifact> CREATOR
    = new Parcelable.Creator<ParcelableArtifact>() {
		public ParcelableArtifact createFromParcel(Parcel in) {
		    BPArtifactId id= new BPArtifactId(in.readString());
		    String name= in.readString();
		    Class<?> type=null;
		    Class<?> componentType=null;
			try {
				type =  Class.forName(in.readString());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
			try {
				String componentTypeName=in.readString();
				if(componentTypeName!=null && !componentTypeName.equals(""))
					componentType= Class.forName(componentTypeName);
			}catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
			return new ParcelableArtifact(id,name,type,componentType);
		}
		
		public ParcelableArtifact[] newArray(int size) {
		    return new ParcelableArtifact[size];
		}
	};

}
